<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\massage\controller;

class IndexWxPay extends \app\ApiRest
{
    protected $app;
    public function __construct(\think\App $app)
    {
        $this->app = $app;
    }
    public function createWeixinPay($paymentApp, $openid, $uniacid, $body, $attach, $totalprice)
    {
        global $_GPC;
        global $_W;
        $setting["mini_appid"] = $paymentApp["app_id"];
        $setting["mini_appsecrept"] = $paymentApp["secret"];
        $setting["mini_mid"] = $paymentApp["payment"]["merchant_id"];
        $setting["mini_apicode"] = $paymentApp["payment"]["key"];
        $setting["apiclient_cert"] = $paymentApp["payment"]["cert_path"];
        $setting["apiclient_cert_key"] = $paymentApp["payment"]["key_path"];
        define("WX_APPID", $setting["mini_appid"]);
        define("WX_MCHID", $setting["mini_mid"]);
        define("WX_KEY", $setting["mini_apicode"]);
        define("WX_APPSECRET", $setting["mini_appsecrept"]);
        define("WX_SSLCERT_PATH", $setting["apiclient_cert"]);
        define("WX_SSLKEY_PATH", $setting["apiclient_cert_key"]);
        define("WX_CURL_PROXY_HOST", "0.0.0.0");
        define("WX_CURL_PROXY_PORT", 0);
        define("WX_REPORT_LEVENL", 0);
        require_once PAY_PATH . "/weixinpay/lib/WxPay.Api.php";
        require_once PAY_PATH . "/weixinpay/example/WxPay.JsApiPay.php";
        $tools = new \JsApiPay();
        $input = new \WxPayUnifiedOrder();
        $input->SetBody($body);
        $input->SetAttach(json_encode($attach));
        $input->SetOut_trade_no($attach["out_trade_no"]);
        $input->SetTotal_fee($totalprice * 100);
        $input->SetTime_start(date("YmdHis"));
        $param_arr = ["i" => $uniacid, "t" => $_GPC["t"], "v" => $_GPC["v"], "is_app" => $paymentApp["is_app"], "n" => APP_MODEL_NAME];
        $reply_path = json_encode($param_arr);
        if(defined("IS_WEIQIN")) {
            $path = "https://" . $_SERVER["HTTP_HOST"] . "/addons/" . APP_MODEL_NAME . "/core2/app/Common/wexinPay.php?params=" . $reply_path;
            $paths = "https://" . $_SERVER["HTTP_HOST"] . "/addons/" . APP_MODEL_NAME . "/core2/app/Common/wexinPay.php?ck=789";
            $a = file_get_contents($paths);
            if($a != 1) {
                $this->errorMsg("发起支付失败");
            }
        } else {
            $path = "https://" . $_SERVER["HTTP_HOST"] . "/wexinPay.php?params=" . $reply_path;
            $paths = "https://" . $_SERVER["HTTP_HOST"] . "/wexinPay.php?ck=789";
            $a = file_get_contents($paths);
            if($a != 1) {
                $this->errorMsg("发起支付失败");
            }
        }
        $this->lb_logOutput("BaseApiPath:-----" . $path);
        $input->SetNotify_url($path);
        if($paymentApp["is_app"] != 1) {
            $input->SetTrade_type("JSAPI");
            $input->SetOpenid($openid);
        } else {
            $input->SetTrade_type("APP");
        }
        $order = \WxPayApi::unifiedOrder($input);
        if(!empty($order["return_code"]) && $order["return_code"] == "FAIL") {
            $this->errorMsg($order["return_msg"]);
        }
        $order["mini_mid"] = $setting["mini_mid"];
        if($paymentApp["is_app"] != 1) {
            $jsApiParameters = $tools->GetJsApiParameters($order);
            $jsApiParameters = json_decode($jsApiParameters, true);
        } else {
            $jsApiParameters = $this->getOrder($order);
        }
        if(!empty($jsApiParameters["return_code"])) {
            $this->errorMsg("发起支付失败");
        }
        return $jsApiParameters;
    }
    public function getOrder($order)
    {
        $data = ["appid" => WX_APPID, "partnerid" => WX_MCHID, "timestamp" => time(), "noncestr" => \WxPayApi::getNonceStr(), "package" => "Sign=WXPay", "prepayid" => $order["prepay_id"]];
        $data["paySign"] = $this->makeSign($data);
        return $data;
    }
    public function makeSign($data)
    {
        $data = array_filter($data);
        ksort($data);
        $string_a = http_build_query($data);
        $string_a = urldecode($string_a);
        $string_sign_temp = $string_a . "&key=" . WX_KEY;
        $sign = md5($string_sign_temp);
        $result = strtoupper($sign);
        return $result;
    }
    public function lb_logOutput($data, $flag = 0)
    {
        if($flag == 0) {
            return NULL;
        }
        if(is_array($data)) {
            $data = json_encode($data);
        }
        $filename = "./" . date("Y-m-d") . ".log";
        $str = date("Y-m-d H:i:s") . "   " . $data . "\r\n";
        file_put_contents($filename, $str, FILE_APPEND | LOCK_EX);
    }
    public function returnPay()
    {
        $this->lb_logOutput("in--mingpianNotify");
        $xmlData = file_get_contents("php://input");
        if(empty($xmlData)) {
            $xmlData = "empty   xmlData";
        }
        $this->lb_logOutput("xmlData in mingpian:-----" . $xmlData);
        $this->lb_logOutput("in-mingpian2");
        global $_GPC;
        $xmlData = file_get_contents("php://input");
        $this->lb_logOutput("in-mingpian-\$xmlData:-----" . $xmlData);
        if(defined("IS_WEIQIN")) {
            $uniacid = $_GPC["i"];
            $is_app = $_GPC["is_app"];
        } else {
            $is_app = $_GET["is_app"];
            $uniacid = $_GET["i"];
        }
        $paymentApp = $this->payConfig($uniacid, $is_app);
        $this->lb_logOutput("in-mingpian-uniacid:-----" . $uniacid);
        $setting["mini_appid"] = $paymentApp["app_id"];
        $setting["mini_appsecrept"] = $paymentApp["secret"];
        $setting["mini_mid"] = $paymentApp["payment"]["merchant_id"];
        $setting["mini_apicode"] = $paymentApp["payment"]["key"];
        $setting["apiclient_cert"] = $paymentApp["payment"]["cert_path"];
        $setting["apiclient_cert_key"] = $paymentApp["payment"]["key_path"];
        define("WX_APPID", $setting["mini_appid"]);
        define("WX_MCHID", $setting["mini_mid"]);
        define("WX_KEY", $setting["mini_apicode"]);
        define("WX_APPSECRET", $setting["mini_appsecrept"]);
        define("WX_SSLCERT_PATH", $setting["apiclient_cert"]);
        define("WX_SSLKEY_PATH", $setting["apiclient_cert_key"]);
        define("WX_CURL_PROXY_HOST", "0.0.0.0");
        define("WX_CURL_PROXY_PORT", 0);
        define("WX_REPORT_LEVENL", 0);
        require_once PAY_PATH . "/weixinpay/lib/WxOrderNotify.php";
        $WxPay = new \WxOrderNotify($this->app);
        $WxPay->Handle(false);
    }
    public function aliNotify()
    {
        $data = $_POST;
        if(empty($data)) {
            $data = $_GET;
        }
        $pay_config = $this->payConfig();
        require_once EXTEND_PATH . "alipay/aop/AopClient.php";
        $aop = new \AopClient();
        $aop->alipayrsaPublicKey = $pay_config["payment"]["ali_publickey"];
        $flag = $aop->rsaCheckV1($data, NULL, "RSA2");
        if(!$flag) {
            return false;
        }
        $data["flag"] = $flag;
        if(!empty($data) && $data["trade_status"] == "TRADE_SUCCESS") {
            $order_model = new \app\massage\model\Order();
            $order_model->orderResult($data["out_trade_no"], $data["trade_no"]);
        }
        return true;
    }
    public function aliNotifyUp()
    {
        $data = $_POST;
        if(empty($data)) {
            $data = $_GET;
        }
        $pay_config = $this->payConfig();
        require_once EXTEND_PATH . "alipay/aop/AopClient.php";
        $aop = new \AopClient();
        $aop->alipayrsaPublicKey = $pay_config["payment"]["ali_publickey"];
        $flag = $aop->rsaCheckV1($data, NULL, "RSA2");
        if(!$flag) {
            return false;
        }
        $data["flag"] = $flag;
        if(!empty($data) && $data["trade_status"] == "TRADE_SUCCESS") {
            $order_model = new \app\massage\model\UpOrderList();
            $order_model->orderResult($data["out_trade_no"], $data["trade_no"]);
        }
        return true;
    }
    public function aliNotifyBalance()
    {
        $data = $_POST;
        if(empty($data)) {
            $data = $_GET;
        }
        $pay_config = $this->payConfig();
        require_once EXTEND_PATH . "alipay/aop/AopClient.php";
        $aop = new \AopClient();
        $aop->alipayrsaPublicKey = $pay_config["payment"]["ali_publickey"];
        $flag = $aop->rsaCheckV1($data, NULL, "RSA2");
        if(!$flag) {
            return false;
        }
        $data["flag"] = $flag;
        if(!empty($data) && $data["trade_status"] == "TRADE_SUCCESS") {
            $order_model = new \app\massage\model\BalanceOrder();
            $order_model->orderResult($data["out_trade_no"], $data["trade_no"]);
        }
        return true;
    }
    public function aliNotifyReseller()
    {
        $order_model = new \app\payreseller\model\Order();
        $data = $_POST;
        if(empty($data)) {
            $data = $_GET;
        }
        $pay_config = $this->payConfig();
        require_once EXTEND_PATH . "alipay/aop/AopClient.php";
        $aop = new \AopClient();
        $aop->alipayrsaPublicKey = $pay_config["payment"]["ali_publickey"];
        $flag = $aop->rsaCheckV1($data, NULL, "RSA2");
        if(!$flag) {
            return false;
        }
        $data["flag"] = $flag;
        if(!empty($data) && $data["trade_status"] == "TRADE_SUCCESS") {
            $order_model->orderResult($data["out_trade_no"], $data["trade_no"]);
        }
        return true;
    }
    public function aliAgentRecharge()
    {
        $order_model = new \app\mobilenode\model\RechargeList();
        $data = $_POST;
        if(empty($data)) {
            $data = $_GET;
        }
        $pay_config = $this->payConfig();
        require_once EXTEND_PATH . "alipay/aop/AopClient.php";
        $aop = new \AopClient();
        $aop->alipayrsaPublicKey = $pay_config["payment"]["ali_publickey"];
        $flag = $aop->rsaCheckV1($data, NULL, "RSA2");
        if(!$flag) {
            return false;
        }
        $data["flag"] = $flag;
        if(!empty($data) && $data["trade_status"] == "TRADE_SUCCESS") {
            $order_model->orderResult($data["out_trade_no"], $data["trade_no"]);
        }
        return true;
    }
    public function aliMemberdiscount()
    {
        $order_model = new \app\memberdiscount\model\OrderList();
        $data = $_POST;
        if(empty($data)) {
            $data = $_GET;
        }
        $pay_config = $this->payConfig();
        require_once EXTEND_PATH . "alipay/aop/AopClient.php";
        $aop = new \AopClient();
        $aop->alipayrsaPublicKey = $pay_config["payment"]["ali_publickey"];
        $flag = $aop->rsaCheckV1($data, NULL, "RSA2");
        if(!$flag) {
            return false;
        }
        $data["flag"] = $flag;
        if(!empty($data) && $data["trade_status"] == "TRADE_SUCCESS") {
            $order_model->orderResult($data["out_trade_no"], $data["trade_no"]);
        }
        return true;
    }
    public function aliBalancediscount()
    {
        $order_model = new \app\balancediscount\model\OrderList();
        $data = $_POST;
        if(empty($data)) {
            $data = $_GET;
        }
        $pay_config = $this->payConfig();
        require_once EXTEND_PATH . "alipay/aop/AopClient.php";
        $aop = new \AopClient();
        $aop->alipayrsaPublicKey = $pay_config["payment"]["ali_publickey"];
        $flag = $aop->rsaCheckV1($data, NULL, "RSA2");
        if(!$flag) {
            return false;
        }
        $data["flag"] = $flag;
        if(!empty($data) && $data["trade_status"] == "TRADE_SUCCESS") {
            $order_model->orderResult($data["out_trade_no"], $data["trade_no"]);
        }
        return true;
    }
}

?>